package cn.com.zero.base.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 日志级别动态刷新
 * @createTime 2022/7/14 15:18
 */
//@Configuration
public class LoggingLevelRefresher {
    private final static Logger logger = LoggerFactory.getLogger(LoggingLevelRefresher.class);
    private final static String LOG_LEVEL_KEY_PREFIX="logging.level.";

//    @Resource
//    private LoggingSystem loggingSystem;
//
//    @NacosConfigListener(dataId = "logging.yaml",groupId = "${spring.application.name}", type = ConfigType.YAML)
//    public void onChangeValue(String newValue){
//        Map<String, Object> loggingYaml = new DefaultYamlConfigParse().parse(newValue);
//        if (CollectionUtils.isEmpty(loggingYaml)) {
//            logger.warn("更改日志配置信息时，YAML文件为空");
//            return;
//        }
//        loggingYaml.forEach((key, value) -> {
//            if (key.startsWith(LOG_LEVEL_KEY_PREFIX)) {
//                // 若获取的日志级别为null，则使用默认的日志级别 info
//                String strLevel = (String) loggingYaml.getOrDefault(key, "info");
//
//                String loggerName = key.replace(LOG_LEVEL_KEY_PREFIX, "");
//                LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
//                loggingSystem.setLogLevel(loggerName, level);
//                logger.warn("更改 {} 的日志级别为：{}", loggerName, level);
//            }
//        });
//    }


}
